package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 赵海波 on 2017/6/23.
 */

@RestController
public class DemoController {


    @Autowired
    DemoService demoService;

    @Autowired(required = false)
    private RedisTemplate redisTemplate;
    @Autowired(required = false)
    private StringRedisTemplate stringRedisTemplate;


    @RequestMapping("/add")
    @ResponseBody
    public String add(){
        User user = new User();
        user.setId(1l);
        user.setUserName("赵海波");
        user.setEmail("493450559@qq.com");
        demoService.add("user_1",user);
        return "ok";
    }

    @RequestMapping("/add_1")
    @ResponseBody
    public String add_1(){
        //添加一个 key
        ValueOperations<String, Object> value = redisTemplate.opsForValue();
        value.set("lp", "hello word");
        //获取 这个 key 的值
        System.out.println("添加一个 key  "+value.get("lp"));
        //添加 一个 hash集合
        HashOperations<String, Object, Object>  hash = redisTemplate.opsForHash();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("name", "lp");
        map.put("age", "26");
        hash.putAll("lpMap", map);
        //获取 map
        System.out.println("添加 一个 hash集合"+hash.entries("lpMap"));




        //添加 一个 list 列表
        ListOperations<String, Object> list = redisTemplate.opsForList();
        list.rightPush("lpList", "lp");
        list.rightPush("lpList", "26");
        //输出 list
        System.out.println("添加 一个 list 列表"+list.range("lpList", 0, 1));


        //添加 一个 set 集合
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        set.add("lpSet", "lp");
        set.add("lpSet", "26");
        set.add("lpSet", "178cm");
        //输出 set 集合
        System.out.println("添加 一个 set 集合"+set.members("lpSet"));

        //添加有序的 set 集合
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        zset.add("lpZset", "lp", 0);
        zset.add("lpZset", "26", 1);
        zset.add("lpZset", "178cm", 2);
        //输出有序 set 集合
        System.out.println("添加有序的 set 集合"+zset.rangeByScore("lpZset", 0, 2));




        return "ok";
    }


    @RequestMapping("/query")
    @ResponseBody
    public String query(){
        User user = demoService.query("user_1");
        return "user:"+"/"+user.getId()+"/"+user.getUserName()+"/"+user.getEmail();
    }
    @RequestMapping("/update")
    @ResponseBody
    public String update(){
        User user = new User();
        user.setId(1l);
        user.setUserName("肖蓓蓓");
        user.setEmail("845307505@qq.com");
        demoService.update("user_1",user);
        System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
        return "ok";
    }


    @RequestMapping("/delete")
    @ResponseBody
    public String delete(){
        demoService.delete("user_1");
        return "ok";
    }



}
